//
//  ViewController.m
//  ceshiNET
//
//  Created by mk on 15/6/24.
//  Copyright (c) 2015年 mk. All rights reserved.
//

#import "ViewController.h"
#import "NetHandler.h"
#import "RequestManager.h"
#import "LORequestManger.h"
#import "ZLPhoto.h"
@interface ViewController ()<ZLPhotoPickerBrowserViewControllerDataSource,ZLPhotoPickerBrowserViewControllerDelegate>
@property (nonatomic, strong)UITextField *text1;
@property (nonatomic, strong)UITextField *text2;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)UISlider *slider;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic , strong) NSMutableArray *assets;
@property (weak,nonatomic) UIScrollView *scrollView;

@end

@implementation ViewController
//- (NSMutableArray *)assets{
//    if (!_assets) {
//        _assets = [NSMutableArray arrayWithArray:@[
//                                                   @"0.png",
//                                                   @"1.png",
//                                                   @"2.png",
//                                                  
//                                                   ]];
//    }
//    return _assets;
//}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *image1 = [UIImage imageNamed:@"111"];
     UIImage *image2 = [UIImage imageNamed:@"123"];
    self.array = [NSMutableArray arrayWithObjects:image1,image2,nil];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 300, 200, 44);
    [button setTitle:@"ceshi" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"test"]];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 400, 200, 44)];
    [self.view addSubview:self.slider];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(300, 400, 50, 44)];
    [self.view addSubview:self.label];
    self.assets = [NSMutableArray array];
   // [self createView];
    
}
- (void)createView{
    //这个属性不能少

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
    [self.view addSubview:scrollView];
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.scrollView = scrollView;
    
    // 属性scrollView
    [self reloadScrollView];

}
- (void)reloadScrollView{
    
    // 先移除，后添加
    [[self.scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSUInteger column = 3;
    // 加一是为了有个添加button
    NSUInteger assetCount = self.assets.count + 1;
    
    CGFloat width = self.view.frame.size.width / column;
    for (NSInteger i = 0; i < assetCount; i++) {
        
        NSInteger row = i / column;
        NSInteger col = i % column;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.frame = CGRectMake(width * col, row * width, width, width);
        
        // UIButton
        if (i == self.assets.count){
            // 最后一个Button
            [btn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(photoSelecte) forControlEvents:UIControlEventTouchUpInside];
        }else{
            // 如果是本地ZLPhotoAssets就从本地取，否则从网络取
            if ([[self.assets objectAtIndex:i] isKindOfClass:[ZLPhotoAssets class]]) {
                [btn setImage:[self.assets[i] thumbImage] forState:UIControlStateNormal];
            }
            btn.tag = i;
            [btn addTarget:self action:@selector(tapBrowser:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [self.scrollView addSubview:btn];
    }
    
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY([[self.scrollView.subviews lastObject] frame]));
}

#pragma mark - 选择图片
- (void)photoSelecte{
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
  //  pickerVc.minCount = 9 - self.assets.count;
    pickerVc.minCount = 100;
    pickerVc.status = PickerViewShowStatusCameraRoll;
    pickerVc.callBack = ^(NSArray *status){
        [self.assets addObjectsFromArray:status];
        [self reloadScrollView];
    };
    
    [pickerVc showPickerVc:self];
}

- (void)tapBrowser:(UIButton *)btn{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
    // 图片游览器
    ZLPhotoPickerBrowserViewController *pickerBrowser = [[ZLPhotoPickerBrowserViewController alloc] init];
    // 淡入淡出效果
    // pickerBrowser.status = UIViewAnimationAnimationStatusFade;
    //    pickerBrowser.toView = btn;
    // 数据源/delegate
    pickerBrowser.delegate = self;
    pickerBrowser.dataSource = self;
    // 当前选中的值
    pickerBrowser.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
    // 展示控制器
    [pickerBrowser show];
}


#pragma mark - <ZLPhotoPickerBrowserViewControllerDataSource>
- (NSInteger)numberOfSectionInPhotosInPickerBrowser:(ZLPhotoPickerBrowserViewController *)pickerBrowser{
    return 1;
}

- (NSInteger)photoBrowser:(ZLPhotoPickerBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{
    return self.assets.count;
}

#pragma mark - 每个组展示什么图片,需要包装下ZLPhotoPickerBrowserPhoto
- (ZLPhotoPickerBrowserPhoto *) photoBrowser:(ZLPhotoPickerBrowserViewController *)pickerBrowser photoAtIndexPath:(NSIndexPath *)indexPath{
    ZLPhotoAssets *imageObj = [self.assets objectAtIndex:indexPath.row];
    // 包装下imageObj 成 ZLPhotoPickerBrowserPhoto 传给数据源
    ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:imageObj];
    
    UIButton *btn = self.scrollView.subviews[indexPath.row];
    photo.toView = btn.imageView;
    // 缩略图
    photo.thumbImage = btn.imageView.image;
    return photo;
}
- (void)buttonClick:(UIButton *)button
{
    
    for (NSInteger i = 0; i < 3; i++) {
        
        NSString *str = @"http://192.168.31.240/MYZD/api/bookingfile";
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png", i]];
//         NSData *imageData = UIImageJPEGRepresentation(image, 1);
     //   NSDictionary *dic2 = @{@"mrfile":@{@"user_id":@"2", @"report_type":@"1",@"mr_id":@"15"}};
        

        NSDictionary *dic2 = @{@"bookingFile":@{@"username":@"11111111111", @"token":@"5871E9FC6E4167FB8A4E2576B08F477B",@"booking_id":@"21"}};
        [RequestManager UPLOADIMAGE:str params:dic2 uploadImage:image  success:^(id response) {
            NSLog(@"response = ~~~~~%@",response);

            for (NSString *key in [response objectForKey:@"errors"]) {
                if (key.length != 0) {
                    NSString *errormsg = [[response objectForKey:@"errors"]objectForKey:key];
                    NSLog(@"%@",errormsg);
                }
            }
            
            
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //        NSLog(@"error = %@",error);
        }];
        
    }

    
    

}

- (void)button{
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
